<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <form class="layui-form layuimini-form" lay-filter="edit-form">
        <input name=id value="" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" autocomplete="off" lay-verify="required" lay-reqtext="用户名不能为空"
                    placeholder="请输入用户名" value="" class="layui-input">
                <tip>填写自己管理账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序值</label>
            <div class="layui-input-block">
                <input type="number" name="sort" autocomplete="off" placeholder="请输入排序值" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="nickName" autocomplete="off" placeholder="请输入昵称" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">手机</label>
            <div class="layui-input-block">
                <input type="number" name="phoneNum" autocomplete="off" lay-verify="required" lay-reqtext="手机不能为空"
                    placeholder="请输入手机" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" name="email" autocomplete="off" placeholder="请输入邮箱" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" pane="">
            <label class="layui-form-label">角色列表</label>
            <div class="layui-input-block" id="roles">
                <!-- <input type="checkbox" name="roles" value="default" title="default"> -->
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="remarks" autocomplete="off" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button id="saveBtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存</button>
                <button type="reset" class="layui-btn layui-btn-danger">重置</button>
                <input type="button" class="layui-btn layui-btn-primary goback" value="返回"/>
            </div>
        </div>
    </form>
    <script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
    <script>
        function initView(obj) {
            console.log(obj);
            var findOneUrl = "/dicms/api/desktop/sys_user/findOne";
            var saveOneUrl = "/dicms/api/desktop/sys_user/save";
            layui.use(['form','common'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    common = layui.common,
                    $ = layui.$;
                if (parent.tableConfig.editMode == 0) {

                } else if (parent.tableConfig.editMode == 1) {
                    console.log(obj.data);
                    let userId = obj.data.id;
                    $.ajax({
                        url: findOneUrl,
                        data: { id: userId },
                        type: 'get',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                    $("#saveBtn").removeAttr('disabled');
                                    $("#saveBtn").removeClass("layui-btn-disabled");
                                });
                            } else {
                                //处理角色列表数据
                                let roles = res.data.roles;
                                let checkboxhtml = '';
                                roles.forEach(function(val){
                                    checkboxhtml+=`<input type="checkbox" name="roles[${val.id}]" value="${val.id}" title="${val.roleName}">`;
                                });
                                $("#roles").html(checkboxhtml);
                                form.render('checkbox',"edit-form");
                                // res.data.roles = {};
                                //勾选生效的角色
                                if(res.data.sysUserRoleIds!=""){
                                    let roleIds = res.data.sysUserRoleIds.split(",");
                                    roleIds.forEach(function(val){
                                        res.data[`roles[${val}]`] = true;
                                    });
                                }
                                console.log(res.data);
                                form.val('edit-form', res.data);
                            }
                        },
                        error: function (res) {
                            console.log(res);
                        }
                    });

                }

                //监听提交
                form.on('submit(saveBtn)', function (data) {
                   
                    data = data.field;
                    console.log(data);
                    let patt = new RegExp(`^\\w*\\[[\\w-]*\\]$`);
                    console.log(patt);
                    let keys = Object.keys(data);
                    for(let i=0;i<keys.length;i++){
                        console.log(keys[i]);
                        if(patt.test(keys[i])){
                            let checkKey = keys[i].substring(0,keys[i].indexOf('['));
                            console.log(checkKey);
                            if(!data[checkKey]){
                                data[checkKey] = [];
                            }
                            data[checkKey].push(data[keys[i]]);
                            console.log("match......"+keys[i]);
                            delete data[keys[i]];
                        }
                    }
                    if(data.roles){
                        data.roleIds = data.roles.join(",");
                    }
                    delete data.roles;
                    console.log(data);
                    if (data.nickName.length > 10) {
                        layer.msg('昵称太长!');
                        return false;
                    }
                    if (data.password !== data.repassword) {
                        layer.msg('两次密码输入不一样!');
                        return false;
                    }
                    $("#saveBtn").attr('disabled','disabled');
                    $("#saveBtn").addClass("layui-btn-disabled");
                    $.ajax({
                        url: saveOneUrl,
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        success: function (res) {
                            console.log(res);
                            if (res.code !== 0) {
                                layer.msg(res.msg, function () {
                                });
                            } else {
                                common.refreshAndClose();
                            }
                        },
                        error: function (res) {
                            console.log(res);
                            $("#saveBtn").removeAttr('disabled');
                            $("#saveBtn").removeClass("layui-btn-disabled");
                        }
                    });
                    return false;
                });

                $(".goback").click(function (e) {
                    common.closeIframe();
                });

            });
        }
    </script>
</body>

</html>